<!--
- Author:   .
- Date:    2018/1/11 0011.
- File:    integral.
-->
<template>
  <div class="Web_Box">
    <div class="integrat2 f-cb">
      <span v-show="type== '2'">当前积分 {{memberInfo.freeze_balance}}</span>
      <span v-show="type == '3'">当前股权积分 {{memberInfo.stock}}</span>
      <!--<router-link :to="{name: 'member-spread'}" class="more">推荐用户购买</router-link>-->
    </div>
    <div class="suborbox f-cb">
      <div class="subornat"><i class="iconfont icon-downdot"></i>
        <select v-model="selectUserLv">
          <option value="">全部类型</option>
          <option :value="$dictionary.userLv.a">批发</option>
          <option :value="$dictionary.userLv.b">花店</option>
          <!--<option :value="$dictionary.userLv.c">零售</option>-->
        </select>
      </div>
      <div class="screenbox" style="width: auto"><a><i class="iconfont icon-shijian" @click="openDatePicker"></i></a><router-link :to="{name: 'search-integral'}">搜索</router-link><router-link :to="{name: 'integral-other', query: {type: type}}">其他</router-link></div>
    </div>
    <div v-infinite-scroll="loadMore"
      infinite-scroll-disabled="false"
      infinite-scroll-distance="0"
      infinite-scroll-immediate-check="false">
      <div class="inter_tab" v-for="history in historyList.data"  v-show="history.type != $dictionary.userLv.c"><a>
        <table width="100%" border="0">
          <tr>
            <td width="50%">类型：{{history.type | userLvTypeName}}</td>
            <td width="50%">状态：{{history.state | integralStatusTxt}}</td>
          </tr>
          <tr>
            <td width="50%">贡献积分：<span class="cr_f83166">￥{{history.freeze_balance}}</span></td>
            <td v-show="history.type == $dictionary.userLv.a && type == '2'">合计：<span class="cr_f83166">￥{{parseFloat(history.order_amount) + parseFloat(history.freeze_balance)}} </span>
            </td>
          </tr>
          <tr>
            <td width="50%" >本金：<span>￥{{history.order_amount}}</span></td>
            <td>购买人：{{history.type == $dictionary.userLv.a ? '自己' : history.member_truename}}</td>
          </tr>
          <tr>
            <td>下单时间：{{history.add_time | timestampToDate('yyyy-MM-dd')}}</td>
            <td>到账时间：{{history.fbaddtime | timestampToDate('yyyy-MM-dd')}}</td>
          </tr>
          <tr v-if="history.multi_rate == '1'" >
            <td colspan="2" width="100%" style="color: #898989;">说明：本金中的{{history.multi_array[0].rate_balance}}返比为{{history.multi_array[0].rate}}，贡献{{history.multi_array[0].rate_freeze_balance}}积分；本金中的{{history.multi_array[1].rate_balance}}返比为{{history.multi_array[1].rate}}，贡献{{history.multi_array[1].rate_freeze_balance}}积分。</td>
          </tr>
        </table>
      </a></div>
    </div>
    <loading-more :allLoaded="historyList.allLoaded" :show="historyList.data.length > 0"></loading-more>
    <empty-data :show="historyList.allLoaded && historyList.data.length <= 0"></empty-data>
    <footer-tab></footer-tab>
    <datetime-picker
      ref="datePicker"
      v-model="currentDate"
      type="date"
      year-format="{value} 年"
      month-format="{value} 月"
      date-format="{value}日"
      @confirm="handleDateChange">
    </datetime-picker>
  </div>
</template>

<script>
  import DatetimePicker from '../../../components/monthPicker'
  import {mapModules, mapRules} from 'vuet'

  export default{
    mixins: [
      mapModules({memberInfo: 'memberInfo'}),
      mapRules({need: ['memberInfo']})
    ],
    data() {
      return {
        type: this.$route.query.type || '2', // 2积分 3股权积分（不包含本金）
        historyList: {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []},
        currentDate: new Date(),
        selectMonth: '',
        selectUserLv: ''
      }
    },
    components: {
      'datetime-picker': DatetimePicker
    },
    created() {
      this.initBalanceHistory()
    },
    computed: {},
    watch: {
      selectUserLv: function (value) {
        this.selectUserLv = value
        this.initBalanceHistory()
      }
    },
    methods: {
      gotoOrderDetail(orderId, isSelf) {
        this.$router.push({name: 'order-detail', params: {id: orderId}, query: {self: isSelf, type: this.type}})
      },
      openDatePicker() {
        this.$refs.datePicker.open()
      },

      handleDateChange(value) {
        let date = new Date(value)
        let dateStr = this.$utils.formatDate(date, 'yyyy-MM-dd')
        this.selectMonth = dateStr
        console.log(dateStr)
        this.initBalanceHistory()
      },
      initBalanceHistory() {
        this.historyList = {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
        this.getIntegralHistory(this.historyList.page, this.historyList.pageSize)
      },
      getIntegralHistory(page, pageSize) {
        this.historyList.loading = true
        let params = {
          type: this.selectUserLv,
          s_time: this.selectMonth,
          page: page,
          pageSize: pageSize
        }
        this.$request.getIntegralHistory(params).then(data => {
          if (data.length < this.historyList.pageSize) {
            this.historyList.allLoaded = true
          }
          this.historyList.data = [...this.historyList.data, ...data]
        }).finally(() => {
          this.historyList.requested = true
          this.historyList.loading = false
        })
      },
      loadMore() {
        if (this.historyList.loading || this.historyList.allLoaded) { // 正在加载 或 已取得全部数据
          return
        }
        setTimeout(() => {
          this.getIntegralHistory(++this.historyList.page, this.historyList.pageSize)
        }, 500)
      }
    }
  }
</script>

<style>

</style>